<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------
 

/**
 * 销售商品ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/goods/quanliGoods.class.php');
quanli_require('module/base/goods/goodsSpec.class.php');

class ajax_goodsSale {
	
    public function execute() {
    	
        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {
        	
            switch($_REQUEST['operFlg']) {
                case 1:	//查询销售商品列表
                    $this->goodsSaleList();
                    break;
                case 2:	//同步
                    $this->synchronization();
                    break;
                case 3:	//商品批量同步
                    $this->batchSynchronization();
                    break;
                case 4:	//修改排序
                	$this->editSort();
                	break;
				case 5:	//切换商品规格
					$this->changeGoodsSpec();
					break;
				default:	
					returnJsonData('参数错误');   
            }

        }

    }

    /**
     * 销售商品列表
     *
     */
    public function goodsSaleList (){
    	
        global $G_SHOP;

        $goodsHandler = new quanliGoods( $G_SHOP->DBCA ) ;
        
        $result_data  = $goodsHandler->searchList( $_REQUEST ) ;

        $result_array = array(
            'page'  => $_REQUEST['page'] ,
            'total' => $_REQUEST['count'] ,
            'rows'  => array()
        );

        if ($result_data) {
            foreach($result_data as &$g_l) {
            	$result_array['rows'][] = array(
                    'id'   => $g_l['postID'],
                    'cell' => array(
						'<a href="javascript: void(0);" onclick="goods.picEdit(' . $g_l['postID'] . ')">图片</a>&nbsp;&nbsp;
						<a href="javascript: void(0);" onclick="goods.skuEdit(' . $g_l['postID'] . ')">SKU</a>&nbsp;&nbsp;
						<a href="javascript: void(0);" onclick="goods.listEdit(' . $g_l['postID'] . ')">基本信息</a>&nbsp;&nbsp;
						<a href="javascript: void(0);" onclick="goods.productEdit(' . $g_l['postID'] . ')">生产信息</a>&nbsp;&nbsp;' ,
                        $g_l['product_code'],
                        $g_l['goodsName'],
                        $g_l['cityName'],
                        $g_l['bigCategoryName'],
                        $g_l['sortOrder']
                    ),
                );
            }
        }

        echo json_encode( $result_array );
    }


    //同步
    function synchronization(){
        global $G_SHOP;
        
		$goodsHandler = new goodsHandler ( $G_SHOP->DBCA ) ;
		$result_array = $goodsHandler->goodsToSale($_REQUEST) ;
        echo json_encode( $result_array );
        
    }

	/**
	 * 商品批量同步 
	 *
	 */
    private function batchSynchronization(){
    	
    	global $G_SHOP;
    	
    	//检查参数
		if (!isset($_REQUEST['postID_str']))
            return array( 'code' => 400, 'message' => '缺少参数' );
            
		//参数切割成字符串
        $str_array = explode('_', $_REQUEST['postID_str']);

        //检查是否已经选择了商品
        if(!$str_array) 
        	return array( 'code' => 400, 'message' => '请选择要同步的商品' );
		
        $goodsHandler = new goodsHandler ($G_SHOP->DBCA) ;
        foreach ($str_array as &$g_l) {
        	$goodsHandler->goodsToSale(array('postID' => $g_l)) ;
        }
        echo json_encode(array('code' => 200 , 'message' => '商品批量同步结束'));
    }
    
    /**
     * 修改排序
     *
     */
    public function editSort(){
    	
    	global $G_SHOP;

    	if (!isset($_REQUEST['postID'], $_REQUEST['sortOrder'], $_REQUEST['cityID'])) {
    		 echo json_encode(
    		 	array(
    		 		'code' => 400 , 
    		 		'message' => '缺少参数'
    		 	)
    		 );
    	}
    	$goodsHandler = new quanliGoods( $G_SHOP->DBCA );
    	$result = $goodsHandler->editSort( $_REQUEST );
    	echo json_encode($result);
    }
    
    /**
     * 切换商品规格
     *
     */
    public function changeGoodsSpec(){
    	
    	global $G_SHOP;
    	
    	if (!isset($_REQUEST['specGroupID'])) {
    		 returnJsonData('参数错误');
    	}
    	
    	$specHandler = new goodsSpec( $G_SHOP->DBCA );
    	$arg_array = array(
    		'specGroupID' => $_REQUEST['specGroupID'],
    		'useFlg' => 1,
    		'sortname' => 'sortOrder',
			'sortorder' => 'asc'
    	);
    	$data = null;
    	$spec_result = $specHandler->searchGoodsSpec($arg_array);
    	if ($spec_result) {
    		$data = $spec_result;
    	}
    	
    	returnJsonData('获取商品规格信息成功', 200, $data);
    }
}
